#include "Room.h"

Room::Room(int roomId, int roomType) : m_numberOfTiles(0), m_numberOfObjects(0)
{
	//Initial the flags of the room
	m_bHasEnemySpawner		= false;
	m_bHasStaticObjects		= false;
	m_bHasPlayerStart		= false;
	m_bHasFountain			= false;
	m_bHasFluxGenerator		= false;
	m_bHasBoss				= false;

	//Provides the information for the room settings
	m_roomId	= roomId;
	m_roomType	= roomType;

	//Randomly generators for a level design room
	srand((unsigned int)time(NULL));

	int roomNumber = (rand() % 4) + 1;

	if(roomNumber == 0 || roomNumber > 4)
	{
		roomNumber = (rand() % 4) + 1;
	} 
	
	else
	{
		//Grabs the file for create the room design
		openFile(roomNumber, true);
	}


	//Creates the room design from the opened text file
	if(!initDesignRoom())
	{
		//Return if the information inside of the
		//creation of the design room fails
		return;
	}
}

Room::~Room()
{
	doorList.clear();
	wallList.clear();
}

bool Room::initDesignRoom()
{
	//The purpose of this function is to randomly
	//select a room from the array of text files
	//when the design room is selected it will be
	//initialized.
	if(currentDesignFile.is_open())
	{
		//Holds the information as to the position of the tile
		int x, y;
		x = 0;
		y = 0;
		RECT R;
		GetClientRect(0, &R);
	
		for(row = 0; row < ROOM_WIDTH; row++)
		{
			x = row * WALL_SIZEX;

			for(col = 0; col < ROOM_HEIGHT; col++)
			{
				y = col * WALL_SIZEY;

				m_tiles[row][col].posX = x;
				m_tiles[row][col].posY = y;

				Vector3D position;

				if(!currentDesignFile.eof())
				{
					switch(currentDesignFile.get())
					{
						case WALL:
							m_tiles[row][col].type = WALL;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							position = Vector3D((float)x, (float)y, 0.0f);
							wallList.push_front(cWall(position));
							break;
						case DOOR:
							m_tiles[row][col].type = DOOR;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							position = Vector3D((float)x, (float)y, 0.0f);
							doorList.push_front(cDoor(position));
							break;
						case GRASS_TILE:
							m_tiles[row][col].type = GRASS_TILE;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							break;
						case CONCRETE_TILE:
							m_tiles[row][col].type = CONCRETE_TILE;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							break;
						case DIRT_TILE:
							m_tiles[row][col].type = DIRT_TILE;
							m_tiles[row][col].bIsSpaceFree = false;
							m_numberOfTiles++;
							break;
						case HOLE_TILE:
							m_tiles[row][col].type = HOLE_TILE;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							break;
						case MUD_TILE:
							m_tiles[row][col].type = MUD_TILE;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							break;
						case WATER_TILE:
							m_tiles[row][col].type = WATER_TILE;
							m_tiles[row][col].bIsSpaceFree = true;
							m_numberOfTiles++;
							break;
						default:
							//Nothing was placed inside the element
							m_tiles[row][col].type = EMPTY_SPACE;
							m_tiles[row][col].bIsSpaceFree = false;
							break;
					}
				}
			}
		}

		currentDesignFile.close();
		return true;
	}

	//If the file is not open return the failure of the room
	return false;
}

bool Room::initRoom()
{ 
	//Perform a check on the room type and
	//set the flags for the room as it is equal to
	//a particular type
	switch(m_roomType)
	{
		case STANDARD_ROOM:
			m_bHasPlayerStart = true;
			m_bHasStaticObjects = true;
			return true;
		case HAZARD_ROOM:
			return true;
		case ENEMY_ROOM:
			m_bHasStaticObjects = true;
			m_bHasEnemySpawner = true;
			return true;
		case ITEM_ROOM:
			return true;
		case FOUNTAIN_ROOM:
			m_bHasFountain = true; 
			return true;
		case BOSS_ROOM:
			return true;
		default:
			//When no additional information is available than
			//return false to stop the creation of the room
			return false;
	}
}
void Room::renderRoom()
{
	for(row = 0; row < ROOM_WIDTH; row++)
	{
		for(col = 0; col < ROOM_HEIGHT; col++)
		{
			D3DXMATRIX translation;
			D3DXMatrixIdentity(&translation);
			D3DXMatrixTranslation(&translation, (float)m_tiles[row][col].posX, (float)m_tiles[row][col].posY, 0.0f);
			g_pGraphics->SetTranformation(translation);

			switch(m_tiles[row][col].type)
			{
				case WALL:
					g_pGraphics->drawSprite(g_pAssets->m_pWall);
					break;
				case DOOR:
					g_pGraphics->drawSprite(g_pAssets->m_pDoor);
					break;
				case GRASS_TILE:
					g_pGraphics->drawSprite(g_pAssets->m_pGrass);
					break;
				case CONCRETE_TILE:
					g_pGraphics->drawSprite(g_pAssets->m_pConcrete);
					break;
				case DIRT_TILE:
					g_pGraphics->drawSprite(g_pAssets->m_pDirt);
					break;
				case MUD_TILE:
					g_pGraphics->drawSprite(g_pAssets->m_pMud);
					break;
				case WATER_TILE:
					g_pGraphics->drawSprite(g_pAssets->m_pWater);
					break;
				default:
					//Render nothing to the screen
					break;
			}
		}
	}
}

void Room::openFile(int roomNumber, bool bIsDesign)
{
	if(bIsDesign)
	{
		switch(roomNumber)
		{
			case 1:
				currentDesignFile.open("roomDesign1.txt", ifstream::in);
				break;
			case 2:
				currentDesignFile.open("roomDesign2.txt", ifstream::in);
				break;
			case 3:
				currentDesignFile.open("roomDesign3.txt", ifstream::in);
				break;
			case 4:
				currentDesignFile.open("roomDesign4.txt", ifstream::in);
				break;
			default:
				break;
		}
	}
	//Do nothing
}